<template>
  <div id="contentBody" class="box-body">
    <div class="box box-primary">
      <div class="box-header with-border">
        <h3 class="box-title">参加工程项目建设情况</h3>
        <div class="box-tools pull-right">
          <button type="button" v-if='isEditable' class="btn btn btn-success" @click="editVisiable = true">
            <i class='fa fa-plus-square-o'>添加参加工程项目建设情况</i>
          </button>
        </div>
      </div>
      <div class="box-body">
        <div class=" form-inline dt-bootstrap" id="example1_wrapper">
          <div class="row">
            <div class="col-sm-12 table-responsive">
              <table class="table table-hover table table-bordered table-striped dataTable">
                <tbody>
                  <tr>
                    <th aria-label="项目名称" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">项目名称</th>
                    <th aria-label="项目级别" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">项目级别</th>
                    <th aria-label="项目单位" style="width: 182px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">项目单位</th>
                    <th aria-label="参与形式" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">参与形式</th>
                    <th aria-label="项目开始时间" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">项目开始时间</th>
                    <th aria-label="项目结束时间" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">项目结束时间</th>
                    <th aria-label="获奖情况" style="width: 181px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">获奖情况</th>
                    <th aria-label="备注" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">备注</th>
                    <th v-if='isEditable' aria-label="操作" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">操作</th>
                  </tr>
                  <tr :key='index' v-for='(value,index) in this.projects' role="row" class="value.cls">
                    <td>{{ value.name }}</td>
                    <td class="text-light" v-text="getprojectlevelname(value.level)"></td>
                    <td>{{ value.depart }}</td>
                    <td class="text-light" v-text="getjointypename(value.jointype)"></td>
                    <td>{{ value.starttime }}</td>
                    <td>{{ value.endtime }}</td>
                    <td>{{ value.reward }}</td>
                    <td>{{ value.comment }}</td>
                    <td v-if='isEditable'>
                      <div class="col-md-2">
                        <button @click="deleterace(index)" class="btn btn-xs">删除</button>
                      </div>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
      <el-dialog title="新增参加工程项目建设情况" :visible.sync="editVisiable" :modal="false" size="small">
        <div class="form-horizontal">
          <div class="box-body">
            <div class="form-group">
              <label class="col-md-2 control-label my-label">项目名称</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editproject.name" placeholder="请输入项目名称">
              </div>
              <label class="col-md-2 control-label my-label">项目级别</label>
              <div class="col-sm-4">
                <select v-model="editproject.level" class="form-control">
                  <option :key='option.id' v-for="option in this.pojectlevels" v-bind:value="option.id">{{ option.name }}</option>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">项目单位</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editproject.depart" placeholder="请输入项目单位">
              </div>
              <label class="col-md-2 control-label my-label">参与形式</label>
              <div class="col-sm-4">
                <select v-model="editproject.jointype" class="form-control">
                  <option :key='option.id' v-for="option in this.jointypes" v-bind:value="option.id">{{ option.name }}</option>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">项目开始时间</label>
              <div class="col-sm-4">
                <MyDatePicker v-bind:date="editproject.starttime" @timeChanged="time=>editproject.starttime = time"></MyDatePicker>
              </div>
              <label class="col-md-2 control-label my-label">项目结束时间</label>
              <div class="col-sm-4">
                <MyDatePicker v-bind:date="editproject.endtime" @timeChanged="time=>editproject.endtime = time"></MyDatePicker>
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">取得成绩</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editproject.reward" placeholder="请输入成绩">
              </div>
              <label class="col-md-2 control-label my-label">备注</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editproject.comment" placeholder="请输入备注">
              </div>
            </div>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editVisiable = false">关 闭</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import MyDatePicker from '../../mycomponents/common/MyDatePicker'
export default {
  name: 'Project',
  props:['projects','editable'],
  data: function () {
    return {
      isEditable:true,
      editVisiable: false,
      editproject: {
        level: 1,
        name: '',
        depart:'',
        jointype:1,
        starttime: '',
        endtime: '',
        reward: '',
        comment: ''
      },
      pojectlevels: [],
      jointypes: []
    }
  },
  components: {
    MyDatePicker
  },
  created:function(){
    if(this.editable != null){
      this.isEditable = this.editable
    }
    this.pojectlevels = this.$store.state.sysconfigs.racelevels
    this.jointypes = this.$store.state.sysconfigs.jointypes
  },
  methods: {
    clearEditData() {
        this.editproject.level = 1,
        this.editproject.name = '',
        this.editproject.depart = '',
        this.editproject.starttime = '',
        this.editproject.endtime = '',
        this.editproject.jointype = 1,
        this.editproject.reward = '',
        this.editproject.comment = ''
    },
    getname(cates, id) {
      for (var index in cates) {
        if (cates[index].id == id) {
          return cates[index].name;
        }
      }
      return ''
    },
    getprojectlevelname(id) {
      return this.getname(this.pojectlevels, id)
    },
    getjointypename(id) {
      return this.getname(this.jointypes, id)
    },
    deleterace(index) {
      this.projects.splice(index, 1)
    },
    deepCopy(o, c) {
      var c = c || {}
      for (var i in o) {
        if (typeof o[i] === 'object') {
          if (o[i].constructor === Array) {
            c[i] = []
          } else {
            c[i] = {}
          }
          this.deepCopy(o[i], c[i])
        } else {
          c[i] = o[i]
        }
      }
      return c
    },
    submit(e) {
      var newPrj = {}
      this.deepCopy(this.editproject, newPrj)
      this.projects.push(newPrj)
      this.clearEditData()
      this.editVisiable = false
    }
  }
}
</script>
<style scoped>
#contentBody{
   min-height: 400px;
}
</style>